<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic%7CNoto+Sans+Simplified+Chinese:300,300italic,400,400italic,700,700italic%7CJetBrains+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/@fancyapps/ui/dist/fancybox/fancybox.css" integrity="sha256-RvRHGSuWAxZpXKV9lLDt2e+rZ+btzn48Wp4ueS3NZKs=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wyatex.online","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="由于兴趣原因想设计一个新的语言。这是一个类似C和Go的语言，为了代码简洁简化一些代码，语言内建大量函数，可以直接调用。允许一定程度的指针访问，提供布尔型，数字型，字符串，指针类型，数组类型，结构化类型，函数类型，切片类型。这是一门静态语言，内存管理模型和c一样需要手工管理。接口使用非侵入的duck type模式。">
<meta property="og:type" content="article">
<meta property="og:title" content="设计一个新的语言-1">
<meta property="og:url" content="https://wyatex.online/uncategorized/%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%96%B0%E7%9A%84%E8%AF%AD%E8%A8%80-1/index.html">
<meta property="og:site_name" content="Wyatex&#96;s blog">
<meta property="og:description" content="由于兴趣原因想设计一个新的语言。这是一个类似C和Go的语言，为了代码简洁简化一些代码，语言内建大量函数，可以直接调用。允许一定程度的指针访问，提供布尔型，数字型，字符串，指针类型，数组类型，结构化类型，函数类型，切片类型。这是一门静态语言，内存管理模型和c一样需要手工管理。接口使用非侵入的duck type模式。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-18T13:27:01.000Z">
<meta property="article:modified_time" content="2023-05-05T01:25:47.681Z">
<meta property="article:author" content="Wyatex">
<meta property="article:tag" content="language">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://wyatex.online/uncategorized/%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%96%B0%E7%9A%84%E8%AF%AD%E8%A8%80-1/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wyatex.online/uncategorized/%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%96%B0%E7%9A%84%E8%AF%AD%E8%A8%80-1/","path":"uncategorized/设计一个新的语言-1/","title":"设计一个新的语言-1"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>设计一个新的语言-1 | Wyatex`s blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Wyatex`s blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">97</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">155</span></a></li><li class="menu-item menu-item-编程"><a href="/programming/" rel="section"><i class="fa fa-book fa-fw"></i>编程</a></li><li class="menu-item menu-item-蹲坑读物"><a href="/%E5%B9%B2%E8%B4%A7/%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%BE%A4%E5%88%86%E4%BA%AB%E8%AE%B0%E5%BD%95/" rel="section"><i class="fa fa-book fa-fw"></i>蹲坑读物</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.</span> <span class="nav-text">基本数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E5%AD%97%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.1.</span> <span class="nav-text">数字类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%89%E7%AC%A6%E5%8F%B7%E6%95%B4%E6%95%B0"><span class="nav-number">1.1.1.</span> <span class="nav-text">有符号整数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A0%E7%AC%A6%E5%8F%B7%E6%95%B4%E6%95%B0"><span class="nav-number">1.1.2.</span> <span class="nav-text">无符号整数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%AE%E7%82%B9%E5%9E%8B"><span class="nav-number">1.1.3.</span> <span class="nav-text">浮点型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%8D%E6%95%B0"><span class="nav-number">1.1.4.</span> <span class="nav-text">复数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6"><span class="nav-number">1.2.</span> <span class="nav-text">字符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#byte%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.2.1.</span> <span class="nav-text">byte类型</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">1.3.</span> <span class="nav-text">字符串</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%87%E7%89%87-slice"><span class="nav-number">1.4.</span> <span class="nav-text">切片 | slice</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.</span> <span class="nav-text">循环语句</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5"><span class="nav-number">3.</span> <span class="nav-text">条件语句</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%B1%A1%E7%89%99%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">4.</span> <span class="nav-text">象牙运算符</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%96%B9%E6%B3%95"><span class="nav-number">5.</span> <span class="nav-text">方法</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%A4%9A%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="nav-number">6.</span> <span class="nav-text">多返回值</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%A4%9A%E8%BF%94%E5%9B%9E%E5%80%BC%E5%8F%96%E5%85%B6%E4%B8%AD%E4%B8%80%E4%B8%AA"><span class="nav-number">7.</span> <span class="nav-text">多返回值取其中一个</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8D%95%E6%9D%A1%E8%AF%AD%E5%8F%A5%E4%BB%A3%E7%A0%81%E6%8D%A2%E8%A1%8C-%EF%BC%88%E5%BE%85%E5%AE%9A%EF%BC%89"><span class="nav-number">8.</span> <span class="nav-text">单条语句代码换行 （待定）</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0"><span class="nav-number">9.</span> <span class="nav-text">匿名函数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%97%AD%E5%8C%85"><span class="nav-number">10.</span> <span class="nav-text">闭包</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8C%85%E7%AE%A1%E7%90%86%E5%92%8C%E5%BC%95%E5%85%A5"><span class="nav-number">11.</span> <span class="nav-text">包管理和引入</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E7%BB%93%E6%9E%84%E4%BD%93%E4%BB%A3%E6%9B%BF%E7%B1%BB"><span class="nav-number">12.</span> <span class="nav-text">使用结构体代替类</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3"><span class="nav-number">13.</span> <span class="nav-text">接口</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Wyatex"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Wyatex</p>
  <div class="site-description" itemprop="description">这里是我用来做笔记的网站哟~</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">155</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">97</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wyatex" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyatex" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wyatex@qq.com" title="E-Mail → mailto:wyatex@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://antfu.me/" title="https:&#x2F;&#x2F;antfu.me&#x2F;" rel="noopener" target="_blank">Anthony Fu</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://www.codesky.me/" title="https:&#x2F;&#x2F;www.codesky.me&#x2F;" rel="noopener" target="_blank">CodeSky</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://learn.lianglianglee.com/" title="https:&#x2F;&#x2F;learn.lianglianglee.com&#x2F;" rel="noopener" target="_blank">技术文章摘抄</a>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wyatex.online/uncategorized/%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%96%B0%E7%9A%84%E8%AF%AD%E8%A8%80-1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Wyatex">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Wyatex`s blog">
      <meta itemprop="description" content="这里是我用来做笔记的网站哟~">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="设计一个新的语言-1 | Wyatex`s blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          设计一个新的语言-1
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-03-18 21:27:01" itemprop="dateCreated datePublished" datetime="2020-03-18T21:27:01+08:00">2020-03-18</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-05-05 09:25:47" itemprop="dateModified" datetime="2023-05-05T09:25:47+08:00">2023-05-05</time>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote>
<p>由于兴趣原因想设计一个新的语言。这是一个类似C和Go的语言，为了代码简洁简化一些代码，语言内建大量函数，可以直接调用。允许一定程度的指针访问，提供布尔型，数字型，字符串，指针类型，数组类型，结构化类型，函数类型，切片类型。这是一门静态语言，内存管理模型和c一样需要手工管理。接口使用非侵入的duck type模式。</p>
</blockquote>
<span id="more"></span>


<h1 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h1><h2 id="数字类型"><a href="#数字类型" class="headerlink" title="数字类型"></a>数字类型</h2><h3 id="有符号整数"><a href="#有符号整数" class="headerlink" title="有符号整数"></a>有符号整数</h3><p>int8    （-128 -&gt; 127）<br>int16   （-32768 -&gt; 32767）<br>int32   （-2,147,483,648 -&gt; 2,147,483,647）<br>int64   （-9,223,372,036,854,775,808 -&gt; 9,223,372,036,854,775,807）<br>int     （32 位操作系统上32位,64 位操作系统64 位）</p>
<h3 id="无符号整数"><a href="#无符号整数" class="headerlink" title="无符号整数"></a>无符号整数</h3><p>uint8   （0 -&gt; 255）<br>uint16  （0 -&gt; 65,535）<br>uint32  （0 -&gt; 4,294,967,295）<br>uint64  （0 -&gt; 18,446,744,073,709,551,615）<br>uint    （32位操作系统上32 位,64位操作系统64位）</p>
<h3 id="浮点型"><a href="#浮点型" class="headerlink" title="浮点型"></a>浮点型</h3><p>float32 （+- 1.2e-38 -&gt; +- 3.4 * 1e38）<br>float64 （+- 2.3e-308 -&gt; 1.7e308）<br>float128（+- 3.5e-4932 -&gt; 1.1e4932）<br>建议使用float64</p>
<h3 id="复数"><a href="#复数" class="headerlink" title="复数"></a>复数</h3><p>complex64 (32 位实数和虚数)<br>complex128 (64 位实数和虚数)</p>
<h2 id="字符"><a href="#字符" class="headerlink" title="字符"></a>字符</h2><h3 id="byte类型"><a href="#byte类型" class="headerlink" title="byte类型"></a>byte类型</h3><p>byte类型：等同于uint8，有三种表达式:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">byte ch = &#x27;A&#x27;</span><br><span class="line">byte ch = 65        //或者byte ch = 0x41</span><br><span class="line">byte ch = &#x27;\x41&#x27;    //字符表达，\x后接两位16进制</span><br></pre></td></tr></table></figure>
<p>rune类型：等同于int32，最少需要int16来储存一个Unicode，两个字节的utf8使用\u前缀，四个字节utf8mb4使用\U来表示。\u紧接四个16进制数，\U紧接八个16进制数。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int ch = &#x27;\u0041&#x27;</span><br><span class="line">int ch = &#x27;\u03B2&#x27;</span><br><span class="line">int ch = &#x27;\U00101234&#x27;</span><br></pre></td></tr></table></figure>

<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><p>字符串定义：<br>struct string<br>{<br>    byte*   str;<br>    int64   len;<br>}</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">string str1 = &quot;This is a string&quot;</span><br><span class="line">str2 := str1 + &quot;Hello World&quot;</span><br><span class="line">// 声明字符串时可以自动根据右边的语句构造一个字符串，但是一个string就是一个原子所以不能像C语言那样用string[x]对第x-1个值进行修改，只能修改地址值，如：</span><br><span class="line">str1 = &quot;This is another string&quot;</span><br><span class="line">// 这样结构体内的str和len值就发生了改变</span><br></pre></td></tr></table></figure>

<h2 id="切片-slice"><a href="#切片-slice" class="headerlink" title="切片 | slice"></a>切片 | slice</h2><p>相当于Java或者Python中的list，能记录长度和容量，也可以理解成一个滑动窗口,但是slice是引用类型所以使用需要注意不要踩坑。<br>底层定义：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">type slice struct &#123;</span><br><span class="line">    pointer array       // pointer是无类型的指针，建议不会用就不要使用，在开发时不应直接使用</span><br><span class="line">    int     len</span><br><span class="line">    int     cap</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">s1 := []string&#123;&quot;&quot;&#125;</span><br></pre></td></tr></table></figure>

<h1 id="循环语句"><a href="#循环语句" class="headerlink" title="循环语句"></a>循环语句</h1><p>在for中，去掉()，加入range用法，如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">int arr[10]</span><br><span class="line">for int i = 0; i &lt; 10; i++ &#123;</span><br><span class="line">    arr[i] = i</span><br><span class="line">&#125;</span><br><span class="line">for index, value := range arr &#123;</span><br><span class="line">    printf(&quot;index = %d, value = %d&quot;, index, value)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="条件语句"><a href="#条件语句" class="headerlink" title="条件语句"></a>条件语句</h1><p>switch默认在句尾加入break，如果需要继续判断下面语句则需要加入<code>fallthrough</code>，如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">int i = 90</span><br><span class="line">switch i &#123;</span><br><span class="line">    case 90: </span><br><span class="line">        printf(&quot;等于90&quot;)</span><br><span class="line">        fallthrough</span><br><span class="line">    case 80: printf(&quot;等于80&quot;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意：由于switch是需要对类型进行判等操作，case后的无符号的表达式结果可以转换成switch后的表达式的类型，反之switch后的无符号的表达式结果不能转换成case后的表达式结果的类型而是转换成该值的默认类型（比如无类型的123转换成int类型，3.14转换成float64类型），所以想让编译通过也是有条件的，如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">int8 arr[] = &#123;0, 1, 2, 3, 4, 5&#125;</span><br><span class="line">switch 1+2 &#123;</span><br><span class="line">    case arr[0], arr[1]:</span><br><span class="line">        printf(&quot;0 or 1&quot;)</span><br><span class="line">    case arr[2], arr[3]:</span><br><span class="line">        printf(&quot;2 or 3&quot;)</span><br><span class="line">    case arr[4], arr[5]:</span><br><span class="line">        printf(&quot;4 or 5&quot;)</span><br><span class="line">&#125;</span><br><span class="line">// 编译不通过，1+3是无符号类型，转换成默认类型是int，而case后是int8类型</span><br><span class="line">switch arr[3] &#123;</span><br><span class="line">    case 0, 1:</span><br><span class="line">        printf(&quot;0 or 1&quot;)</span><br><span class="line">    case 2, 3:</span><br><span class="line">        printf(&quot;2 or 3&quot;)</span><br><span class="line">    case 4, 5:</span><br><span class="line">        printf(&quot;4 or 5&quot;)</span><br><span class="line">&#125;</span><br><span class="line">// 编译通过，因为case后的无类型值可以转换成int8类型。</span><br><span class="line">// 注意：case后的类型只有在无类型时才能发生类型自动转换</span><br></pre></td></tr></table></figure>

<h1 id="象牙运算符"><a href="#象牙运算符" class="headerlink" title="象牙运算符"></a>象牙运算符</h1><p>直接由编译器判断类型，同时声明和定义一个变量，如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">i := 10</span><br><span class="line">str := &quot;string&quot;</span><br><span class="line">arr := &#123;1, 2, 3&#125;</span><br></pre></td></tr></table></figure>

<h1 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h1><p>和函数不一样，必须拥有一个接收者，代码操作和接收者绑定，如</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int (int i)returnANum() &#123;       // i为接收者，操作与i绑定</span><br><span class="line">    return i</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h1 id="多返回值"><a href="#多返回值" class="headerlink" title="多返回值"></a>多返回值</h1><p>一个函数或者方法可以返回多个值，如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">(int, float32)returnANum()&#123;</span><br><span class="line">    float32 f = 13.14</span><br><span class="line">    i = 1</span><br><span class="line">    return i, f</span><br><span class="line">&#125;</span><br><span class="line">(string str, int i)(int i)returnAString() &#123;</span><br><span class="line">    string str = &quot;This is a string&quot;</span><br><span class="line">    return</span><br><span class="line">&#125;</span><br><span class="line">i, f := returnANum()          // i=1, f=13.14</span><br><span class="line">str, j := i.returnAString()     //str=&quot;This is a string&quot;, j = 1</span><br></pre></td></tr></table></figure>

<h1 id="多返回值取其中一个"><a href="#多返回值取其中一个" class="headerlink" title="多返回值取其中一个"></a>多返回值取其中一个</h1><p>可以在函数或者方法后接<code>.返回值</code>可以只取一个，或者用<code>_</code>来抛弃返回值，如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">// func()返回</span><br><span class="line">max := func(a, b, c).max</span><br><span class="line">sum, _ := func(a, b, c)</span><br></pre></td></tr></table></figure>

<h1 id="单条语句代码换行-（待定）"><a href="#单条语句代码换行-（待定）" class="headerlink" title="单条语句代码换行 （待定）"></a>单条语句代码换行 （待定）</h1><p>如果不是双引号的代码可以在逗号后面直接换行，如果是字符串需要多行可以用反单引号或者加号，如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">printf(&quot;a = %d, b = %d&quot;, </span><br><span class="line">        a, b)</span><br><span class="line"></span><br><span class="line">str := `&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=&quot;en&quot;&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;title&gt;Document&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt; </span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;` +</span><br><span class="line">&quot;this is a html5 code&quot;</span><br></pre></td></tr></table></figure>

<h1 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h1><p>可以声明一个匿名函数，还可以把他赋值给一个变量，比如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">func := void &#123;</span><br><span class="line">    printf(&quot;hello world!&quot;)</span><br><span class="line">&#125;</span><br><span class="line">func()          // 输出hello world！</span><br><span class="line"></span><br><span class="line">void (int x, int y)&#123;</span><br><span class="line">    printf(&quot;sum = %d&quot;, x, y)</span><br><span class="line">&#125;(10, 20)       // 输出sum = 30</span><br></pre></td></tr></table></figure>

<h1 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h1><p>如：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">typedef operate int (int, int)      // 定义一个返回值为int，参数列表为两个int值的函数类型</span><br><span class="line">typedef calculate int (int, int)</span><br><span class="line"></span><br><span class="line">calculate getSum(op operate) &#123;</span><br><span class="line">    return  int(int x, int y)&#123;</span><br><span class="line">                return op(x, y)</span><br><span class="line">            &#125;</span><br><span class="line">&#125;</span><br><span class="line">add := int (int x, int y)&#123;</span><br><span class="line">    return x + y</span><br><span class="line">&#125;</span><br><span class="line">addFunc := getSum(add)</span><br><span class="line">printf(&quot;sum = %d&quot;, addFunc(10, 20))     // 输出sum=30</span><br></pre></td></tr></table></figure>

<h1 id="包管理和引入"><a href="#包管理和引入" class="headerlink" title="包管理和引入"></a>包管理和引入</h1><p>待定</p>
<h1 id="使用结构体代替类"><a href="#使用结构体代替类" class="headerlink" title="使用结构体代替类"></a>使用结构体代替类</h1><p>这个得看怎么解决包管理</p>
<h1 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h1><p>空接口</p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Wyatex
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wyatex.online/uncategorized/%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E6%96%B0%E7%9A%84%E8%AF%AD%E8%A8%80-1/" title="设计一个新的语言-1">https://wyatex.online/uncategorized/设计一个新的语言-1/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/language/" rel="tag"># language</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/%E5%89%8D%E7%AB%AF/CSS%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-1/" rel="prev" title="CSS学习笔记-1">
                  <i class="fa fa-angle-left"></i> CSS学习笔记-1
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/%E5%B9%B2%E8%B4%A7/%E4%B8%80%E4%BA%9B%E6%9C%89%E7%94%A8%E7%9A%84%E7%BD%91%E7%AB%99%E6%8E%A8%E8%8D%90%EF%BC%882%EF%BC%89/" rel="next" title="一些有用的网站推荐（2）">
                  一些有用的网站推荐（2） <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2019 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Wyatex</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/ribbon.js/dist/ribbon.min.js"></script>
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="/lib/@next-theme/pjax/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="/lib/@fancyapps/ui/dist/fancybox/fancybox.umd.js" integrity="sha256-q8XkJ6dj5VwSvzI8+nATCHHQG+Xv/dAZBCgqmu93zOY=" crossorigin="anonymous"></script>
  <script src="/lib/lozad/dist/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
